package com.example.route.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.route.entity.Route;
import com.example.route.mapper.RouteMapper;
import com.example.route.service.RouteService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class RouteServiceImpl extends ServiceImpl<RouteMapper,Route> implements RouteService {

    private static List<Route> routeList = new ArrayList<>();

    private int routeCount = 1;

    @Override
    public List<String> findRoutes(String start, String end) {
        return findRoutes(start,end,"",new ArrayList<String>());
    }

    private List<String> findRoutes(String start, String end, String result, List<String> resultList) {
        if(routeList.size()==0){
            routeCount = 1;
            routeList = this.list(new QueryWrapper<Route>());
        }
        for(Route route:routeList){
            if(result.contains(route.getName())){
                continue;
            }
            if((start.equals(route.getStartId())&&end.equals(route.getEndId()))||(start.equals(route.getEndId())&&end.equals(route.getStartId()))){
                result = routeCount++ +": "+route.getName()+"-"+result.substring(0,result.length()-1);
                resultList.add(result);
            }else if(route.getStartId().equals(end)){
                findRoutes(start,route.getEndId(),route.getName()+"-"+result,resultList);
            }else if(route.getEndId().equals(end)){
                findRoutes(start,route.getStartId(),route.getName()+"-"+result,resultList);
            }
        }
        return resultList;
    }

    private static int count = 1;

    public static List<String> test(String start,String end){
        return test(start,end,"",new ArrayList<String>());
    }

    public static List<String> test(String start, String end, String result, List<String> resultList){
        for(Route route:routeList){
            if(result.contains(route.getName())){
                continue;
            }
            if(result.contains("-节点六")){
                String s = "";
            }
            if((start.equals(route.getStartId())&&end.equals(route.getEndId()))||(start.equals(route.getEndId())&&end.equals(route.getStartId()))){
               resultList.add(count++ +": "+route.getName()+"-"+result.substring(0,result.length()-1));
            }else if(route.getStartId().equals(end)){
                test(start,route.getEndId(),route.getName()+"-"+result,resultList);
            }else if(route.getEndId().equals(end)){
                test(start,route.getStartId(),route.getName()+"-"+result,resultList);
            }
        }
        return resultList;
    }

    public static void main(String[] args){
        Route route1 = new Route("1", "节点一", "a", "b");
        Route route2 = new Route("2", "节点二", "b", "c");
        Route route3 = new Route("3", "节点三", "c", "d");
        Route route4 = new Route("4", "节点四", "d", "e");
        Route route5 = new Route("5", "节点五", "c", "b");
        Route route6 = new Route("6", "节点六", "b", "e");

        routeList.add(route1);
        routeList.add(route2);
        routeList.add(route3);
        routeList.add(route4);
        routeList.add(route5);
        routeList.add(route6);

        List<String> result = test("a","e");
        System.out.println("从a到e");
        for(String str:result){
            System.out.println(str);
        }

        List<String> result2 = test("e","a");
        System.out.println("从e到a");
        for(String str:result2){
            System.out.println(str);
        }
    }
}
